package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.DTO.operate.OperatePropertyPackageDTO;
import com.itlong.cloud.POJO.PO.PropertyPackagePO;
import com.itlong.cloud.POJO.PO.PropertyRoleFunctionPO;
import com.itlong.cloud.POJO.VO.operate.OperatePropertyAccountDetailVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.operate.dao.IOperatePropertyPackageDao;
import com.itlong.cloud.operate.service.IOperatePropertyPackageService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <desc>
 *      运营-套餐管理-物业套餐管理管理业务处理实现类
 * </desc>
 *
 * @createDate2020/3/23
 **/
@Service
public class OperatePropertyPackageServiceImpl implements IOperatePropertyPackageService {

    @Autowired
    private IOperatePropertyPackageDao iOperatePropertyPackageDao;

    /**
     * <desc>
     *      获取物业套餐列表
     * </desc>
     *
     * @param operatePropertyPackageDTO 查询参数实体
     * @return
     * @Author Zhu.yj
     * @createDate  2020/3/24
     */
    @Override
    public Page<PropertyPackagePO> getPage(OperatePropertyPackageDTO operatePropertyPackageDTO) throws Exception {
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(operatePropertyPackageDTO);
        Page<PropertyPackagePO> page = new Page<>();
        page.getPage().setPageSize(operatePropertyPackageDTO.getPageSize());
        page.getPage().setCurrentPage(operatePropertyPackageDTO.getCurrentPage());
        params.put("startSlip",page.getPage().getStartSlip());
        params.put("pageSize",page.getPage().getPageSize());
        page.setRows(iOperatePropertyPackageDao.getPage(params),iOperatePropertyPackageDao.getPageCount(params));
        return page;
    }


    /**
     * <desc>
     *      更新物业套餐信息
     * </desc>
     *
     * @param propertyPackagePO 参数实体
     * @return
     * @Author Zhu.yj
     * @createDate  2020/3/24
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer update(PropertyPackagePO propertyPackagePO) throws Exception {
        propertyPackagePO.setUpdateTime(new Date());
        return iOperatePropertyPackageDao.update(SqlUtil.durableData(propertyPackagePO, PlatformConstants.TABLE_UPDATE));
    }

    /**
     * <desc>
     *      保存物业套餐信息
     * </desc>
     *
     * @param propertyPackagePO 参数实体
     * @return
     * @Author Zhu.yj
     * @createDate  2020/3/24
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer save(PropertyPackagePO propertyPackagePO) throws Exception {
        propertyPackagePO.setPropertyPackageId(LogicIdUtil.bussinessId());
        propertyPackagePO.setStatus(DataStatusEnum.NORMAL.getType());
        propertyPackagePO.setCreateTime(new Date());
        propertyPackagePO.setUpdateTime(new Date());
        return iOperatePropertyPackageDao.save(SqlUtil.durableData(propertyPackagePO,PlatformConstants.TABLE_SAVE));
    }

    /**
     * <desc>
     *      批量删除物业套餐(弃用)
     * </desc>
     *
     * @param propertyPackageIds 物业套餐id，多个逗号隔开
     * @return
     * @Author Zhu.yj
     * @createDate  2020/3/24
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer delete(String propertyPackageIds) throws Exception {
        String[] ids = StringHandlerUtil.splitString(propertyPackageIds);
        //删除套餐关联的账号
        iOperatePropertyPackageDao.deleteByPackageIds(ids);
        //删除套餐绑定的菜单
        iOperatePropertyPackageDao.deleteFunctions(ids);
        return iOperatePropertyPackageDao.delete(ids);
    }

//    /**
//     * <desc>
//     *      获取物业套餐绑定的账号
//     * </desc>
//     *
//     * @param propertyPackageId 物业套餐id
//     * @return
//     * @Author Zhu.yj
//     * @createDate  2020/3/24
//     */
//    @Override
//    @Transactional(propagation = Propagation.REQUIRED)
//    public List<String> getPropertyPackageAccount(String propertyPackageId) throws Exception {
//        return iOperatePropertyPackageDao.getPropertyPackageAccount(propertyPackageId);
//    }

//    /**
//     * <desc>
//     *      获取物业账号下的套餐列表
//     * </desc>
//     *
//     * @return 套餐id，套餐名称
//     * @Author Zhu.yj
//     * @createDate  2020/3/25
//     */
//    @Override
//    public List<OperatePropertyPackageVO> getPackage(String projectId) throws Exception {
//        return iOperatePropertyPackageDao.getPackage(projectId);
//    }

    /**
     * <desc>
     *      物业套餐分配功能
     * </desc>
     *
     * @param propertyFunctionIds 物业功能菜单id，多个逗号隔开
     * @param propertyPackageId  物业套餐id
     * @return
     * @Author Zhu.yj
     * @createDate  2020/3/25
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer allotPropertyFunction(String propertyFunctionIds, String propertyPackageId) throws Exception{
//        Date nowTime = new Date();
//        //获取旧的功能列表
//        List<String> functionList = iOperatePropertyPackageDao.getFunctionIds(propertyPackageId);
//        //变更套餐的功能
//        String[] propertyPackageIds = StringHandlerUtil.splitString(propertyPackageId);
//        iOperatePropertyPackageDao.deleteFunctions(propertyPackageIds);
//        List<String> propertyFunctionIdList = StringHandlerUtil.splitStringList(propertyFunctionIds);
//        if (!propertyFunctionIdList.isEmpty()){
//           iOperatePropertyPackageDao.allotFunction(propertyFunctionIdList,propertyPackageId,DataStatusEnum.NORMAL.getType(),new Date());
//        }
//
//        //找出该套餐绑定的项目，变更该项目下物业账号的菜单权限
//        List<Map<String, Object>> projectList = iOperatePropertyPackageDao.getPropertyPackageAccount(propertyPackageId);
//        if (!projectList.isEmpty()){
//            //删除套餐绑定项目下的物业管理员账号与集团物业管理员的菜单权限
//            iOperatePropertyPackageDao.removeRoleProjectFunction(projectList);
//
//            //将这些项目下的其它物业账号的菜单权限变更
//            functionList.removeAll(propertyFunctionIdList);
//            //重新分配
//            List<PropertyRoleFunctionPO> propertyRoleFunctionPOS = new ArrayList<>();
//            List<PropertyRoleFunctionPO> propertyRoleFunctionPODeleteList = new ArrayList<>();
//            for (Map<String, Object> projectIdMap : projectList){
//                //要添加的
//                PropertyRoleFunctionPO propertyRoleFunctionPO = new PropertyRoleFunctionPO();
//                propertyRoleFunctionPO.setRoleId(projectIdMap.get("roleId").toString());
//                propertyRoleFunctionPO.setProjectId(projectIdMap.get("projectId").toString());
//                propertyRoleFunctionPO.setCreateTime(nowTime);
//                propertyRoleFunctionPO.setUpdateTime(nowTime);
//                for (String functionId : propertyFunctionIdList){
//                    propertyRoleFunctionPO.setFunctionId(functionId);
//                    propertyRoleFunctionPOS.add(propertyRoleFunctionPO);
//                }
//
//                //要删除的
//                PropertyRoleFunctionPO propertyRoleFunctionPODelete = new PropertyRoleFunctionPO();
//                propertyRoleFunctionPODelete.setProjectId(projectIdMap.get("projectId").toString());
//                for (String functionId : functionList){
//                    propertyRoleFunctionPODelete.setFunctionId(functionId);
//                    propertyRoleFunctionPODeleteList.add(propertyRoleFunctionPODelete);
//                }
//            }
//            iOperatePropertyPackageDao.saveRoleProjectFunction(propertyRoleFunctionPOS);
//
//            if (StringUtils.isNotBlank(propertyRoleFunctionPODeleteList.get(0).getFunctionId())){
//                iOperatePropertyPackageDao.removeOtherPropertyFunction(propertyRoleFunctionPODeleteList);
//            }
//
//        }
//        Map<String, Object> params = new HashMap<>();
//        params.put("propertyPackageId",propertyPackageId);
//        params.put("updateTime",nowTime);
//        iOperatePropertyPackageDao.updateOperateTime(params);
        return 1;
    }

//    /**
//     * <desc>
//     *      分配套餐用户（待删除）
//     * </desc>
//     *
//     * @param propertyPackageId 物业套餐id
//     * @param allotUserIds 选择的用户id，多个逗号隔开
//     * @param userIds 当前页面所有用户id
//     * @return
//     * @Author Zhu.yj
//     * @createDate  2020/3/26
//     */
//    @Override
//    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
//    public Integer allotPackageUser(String propertyPackageId, String allotUserIds, String userIds) throws Exception{
//        //筛选出没有勾选的用户
//        String[] userIdList = StringHandlerUtil.splitString(userIds);
//        String[] allotUserId=StringHandlerUtil.splitString(allotUserIds);
//        //先将当前套餐id关联当前页所有用户id全部删掉
//        iOperatePropertyPackageDao.deletePackageAccount(propertyPackageId,userIdList);
//        //给要分配的用户分配该套餐
//        if (allotUserId.length > 0){
//            iOperatePropertyPackageDao.allotPackageUser(propertyPackageId,allotUserId,DataStatusEnum.NORMAL.getType(),new Date());
//        }
//        //将当前页所有用户id的此套餐id关联的菜单删掉
//        Arrays.asList(userIdList).forEach(userId -> {
//            //删除账号在不同项目下的关于该套餐的功能
//            List<String> packageVOList = iOperatePropertyPackageDao.getPackageIdByUserId(userId);
//            OperatePropertyAccountDetailVO operatePropertyAccountDetailVO = iOperatePropertyPackageDao.getDetail(userId);
//            //先删除该用户角色菜单
//            iOperatePropertyPackageDao.removeRoleProjectFunction(operatePropertyAccountDetailVO.getRoleId(),
//                    StringHandlerUtil.splitStringList(operatePropertyAccountDetailVO.getProjectIds()));
//            //添加用户角色菜单
//            packageVOList.add(propertyPackageId);
//            List<String> functionIds = iOperatePropertyPackageDao.getFunctionIds(packageVOList);
//            List<PropertyRoleFunctionPO> roleFunctionPOS = new ArrayList<>();
//            for (String funcId : functionIds){
//                for (String projId : StringHandlerUtil.splitString(operatePropertyAccountDetailVO.getProjectIds())){
//                    PropertyRoleFunctionPO functionPO = new PropertyRoleFunctionPO(
//                            operatePropertyAccountDetailVO.getRoleId(),
//                            projId.trim(),
//                            funcId.trim()
//                    );
//                    roleFunctionPOS.add(functionPO);
//                    if (roleFunctionPOS.size() == 100){
//                        iOperatePropertyPackageDao.saveRoleProjectFunction(roleFunctionPOS);
//                        roleFunctionPOS.clear();
//                    }
//                }
//            }
//            if (!roleFunctionPOS.isEmpty()){
//                iOperatePropertyPackageDao.saveRoleProjectFunction(roleFunctionPOS);
//            }
//        });
//        Map<String, Object> params = new HashMap<>();
//        params.put("propertyPackageId",propertyPackageId);
//        params.put("updateTime",new Date());
//        iOperatePropertyPackageDao.updateOperateTime(params);
//        return 1;
//    }

    /**
     * <desc>
     *      获取单个物业套餐信息
     * </desc>
     *
     * @param propertyPackageId 物业套餐id
     * @return  单个物业套餐详细信息
     * @Author Zhu.yj
     * @createDate  2020/4/1
     */
    @Override
    public PropertyPackagePO getOne(String propertyPackageId) throws Exception {
        return iOperatePropertyPackageDao.getOne(propertyPackageId);
    }

    /**
     * <desc>
     *      获取绑定了套餐的用户列表
     * </desc>
     *
     * @param propertyPackageId 物业套餐id
     * @return
     * @Author Zhu.yj
     * @createDate  2020/4/3
     */
    @Override
    public List<String> getOther(String propertyPackageId) throws Exception {
        return iOperatePropertyPackageDao.getOther(propertyPackageId);
    }
}
